import requests
import re
import csv
url = 'https://movie.douban.com/top250'

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
}
# 请求拿到页面源代码
res = requests.get(url, headers=headers)
strHtml = res.text
# 使用正则获取到想要的数据
obj = re.compile(r'<li>.*?<div class="item">.*?<span class="title">(?P<name>.*?)</span>.*?<p class="">.*?<br>(?P<year>.*?)&nbsp', re.S)
res1 = obj.finditer(strHtml)
f = open("data.csv", mode="w", encoding="utf-8")
csvWriter = csv.writer(f)
for s in res1:
    # print(s.group("name"))
    # print(s.group("year").strip())
    dic = s.groupdict()
    dic['year'] = dic['year'].strip()
    csvWriter.writerow(dic.values())
res.close()
print("over!")
